// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import org.joml.Vector4f;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
import zombie.core.SpriteRenderer;
import zombie.core.opengl.Shader;
import zombie.core.opengl.ShaderProgram;
import zombie.iso.sprite.SkyBox;

public final class PuddlesShader extends Shader {
    private int WaterGroundTex;
    private int PuddlesHM;
    private int WaterTextureReflectionA;
    private int WaterTextureReflectionB;
    private int WaterTime;
    private int WaterOffset;
    private int WaterViewport;
    private int WaterReflectionParam;
    private int PuddlesParams;

    public PuddlesShader(String name) {
        super(name);
    }

    @Override
    protected void onCompileSuccess(ShaderProgram shaderProgram) {
        int _int = shaderProgram.getShaderID();
        this.WaterGroundTex = ARBShaderObjects.glGetUniformLocationARB(_int, "WaterGroundTex");
        this.WaterTextureReflectionA = ARBShaderObjects.glGetUniformLocationARB(_int, "WaterTextureReflectionA");
        this.WaterTextureReflectionB = ARBShaderObjects.glGetUniformLocationARB(_int, "WaterTextureReflectionB");
        this.PuddlesHM = ARBShaderObjects.glGetUniformLocationARB(_int, "PuddlesHM");
        this.WaterTime = ARBShaderObjects.glGetUniformLocationARB(_int, "WTime");
        this.WaterOffset = ARBShaderObjects.glGetUniformLocationARB(_int, "WOffset");
        this.WaterViewport = ARBShaderObjects.glGetUniformLocationARB(_int, "WViewport");
        this.WaterReflectionParam = ARBShaderObjects.glGetUniformLocationARB(_int, "WReflectionParam");
        this.PuddlesParams = ARBShaderObjects.glGetUniformLocationARB(_int, "PuddlesParams");
        this.Start();
        if (this.WaterGroundTex != -1) {
            ARBShaderObjects.glUniform1iARB(this.WaterGroundTex, 0);
        }

        if (this.WaterTextureReflectionA != -1) {
            ARBShaderObjects.glUniform1iARB(this.WaterTextureReflectionA, 1);
        }

        if (this.WaterTextureReflectionB != -1) {
            ARBShaderObjects.glUniform1iARB(this.WaterTextureReflectionB, 2);
        }

        if (this.PuddlesHM != -1) {
            ARBShaderObjects.glUniform1iARB(this.PuddlesHM, 3);
        }

        this.End();
    }

    public void updatePuddlesParams(int userId, int z) {
        IsoPuddles isoPuddles = IsoPuddles.getInstance();
        SkyBox skyBox = SkyBox.getInstance();
        PlayerCamera playerCamera = SpriteRenderer.instance.getRenderingPlayerCamera(userId);
        GL13.glActiveTexture(33985);
        skyBox.getTextureCurrent().bind();
        GL11.glTexParameteri(3553, 10240, 9729);
        GL11.glTexParameteri(3553, 10241, 9729);
        GL11.glTexEnvi(8960, 8704, 7681);
        GL13.glActiveTexture(33986);
        skyBox.getTexturePrev().bind();
        GL11.glTexParameteri(3553, 10240, 9729);
        GL11.glTexParameteri(3553, 10241, 9729);
        GL11.glTexEnvi(8960, 8704, 7681);
        GL13.glActiveTexture(33987);
        isoPuddles.getHMTexture().bind();
        GL11.glTexParameteri(3553, 10240, 9729);
        GL11.glTexParameteri(3553, 10241, 9729);
        GL11.glTexEnvi(8960, 8704, 7681);
        ARBShaderObjects.glUniform1fARB(this.WaterTime, isoPuddles.getShaderTime());
        Vector4f vector4f = isoPuddles.getShaderOffset();
        ARBShaderObjects.glUniform4fARB(this.WaterOffset, vector4f.x - 90000.0F, vector4f.y - 640000.0F, vector4f.z, vector4f.w);
        ARBShaderObjects.glUniform4fARB(
            this.WaterViewport,
            (float)IsoCamera.getOffscreenLeft(userId),
            (float)IsoCamera.getOffscreenTop(userId),
            (float)playerCamera.OffscreenWidth / playerCamera.zoom,
            (float)playerCamera.OffscreenHeight / playerCamera.zoom
        );
        ARBShaderObjects.glUniform1fARB(this.WaterReflectionParam, skyBox.getTextureShift());
        ARBShaderObjects.glUniformMatrix4fvARB(this.PuddlesParams, true, isoPuddles.getPuddlesParams(z));
    }
}
